
package com.key.controller.admin.systemSetting;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.vo.SysUserVo;
import com.key.entity.Employee;
import com.key.entity.SysRole;
import com.key.entity.SysUser;
import com.key.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.Arrays;
import java.util.List;

/**
 * @Author：朕
 * @Package：com.key.controller.admin.systemSetting
 * @Project：ams_system
 * @name：SysUser
 * @Date：2023/2/18 9:05
 * @Filename：SysUser
 */
@Controller
@Api(value = "系统设置",tags = "系统用户API")
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;




    @RequestMapping(value = "/findSysUser",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "系统用户的显示",notes = "",httpMethod = "get", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",paramType = "query",dataType = "int",required = true)
    })
    public R findSysUser(Integer currentPage, Integer pageSize){
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<SysUserVo> sysUsers= sysUserService.findSysUser();
        if(sysUsers.size()>0){
            r.setCode(200);
            r.setMsg("显示成功");
            r.setData(sysUsers);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("显示失败");
            return r;
        }
    }


    @RequestMapping(value = "/addSysUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统用户的增加",notes = "传递用户实体类",httpMethod = "post", response = R.class)
    public R addSysUser(@RequestBody @ApiParam(name = "SysUser",value = "系统用户实体",required = true)SysUser sysUser){
        R r = new R();
        boolean s = sysUserService.addSysUser(sysUser);
        if(s==true){
            r.setCode(200);
            r.setMsg("增加成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("增加失败");
            return r;
        }
    }


    @RequestMapping(value = "/deleteSysUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统用户的删除",notes = "用户ID",httpMethod = "post", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserId",value = "用户Id",required = true,dataType = "int",paramType = "query")
    })
    public R deleteSysUser(Integer userId){
        R r = new R();
        boolean s = sysUserService.deleteSysUser(userId);
        if(s == true){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("删除失败");
            return r;
        }
    }


    @RequestMapping(value = "/findSysUserByAccount",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据账号查询系统用户",notes = "传递账号信息",httpMethod = "get", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "identification",value = "账号或名称",paramType = "query",dataType = "String",required = true)
    })
    public R findSysUserByAccount(Integer currentPage,Integer pageSize,String identification){
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<SysUser> sysUsers = sysUserService.findSysUserByAccountAndName(identification);
        if(sysUsers != null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(sysUsers);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("查询失败");
            return r;
        }
    }


    @RequestMapping(value = "/updateSysUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统用户的修改",notes = "User实体类",httpMethod = "post", response = R.class)
    public R updateSysUser(@RequestBody @ApiParam(name = "SysUser",value = "系统用户实体",required = true) SysUser sysUser){
        R r = new R();
        boolean s = sysUserService.updateUser(sysUser);
        if(s==true){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("修改失败");
            return r;
        }
    }


    @RequestMapping(value = "/findRoleByUserId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据用户Id查询相应角色",notes = "userId",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",paramType = "query",dataType = "int",required = true),
    })
    public R findRoleByUserId(Integer userId){
        R r = new R();
        List<SysRole> roles = sysUserService.findRoleByUserId(userId);
        if(roles != null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(roles);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("查询失败");
            return r;
        }
    }


    @RequestMapping(value = "/addUserRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加用户角色",notes = "传递用户ID和角色ID",httpMethod = "post",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "roleId",value = "角色Id",required = true,dataType = "String",paramType = "query")
    })
    public R addUserRole(@RequestParam("userId") Integer userId,@RequestParam("roleId") Integer[] roleId){
        R r = new R();
        System.out.println(Arrays.toString(roleId));
        System.out.println(userId);
        boolean s = sysUserService.deleteAddUserRole(userId, roleId);
        if(s==true){
            r.setCode(200);
            r.setMsg("获取角色成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("获取角色失败");
            return r;
        }
    }


    @RequestMapping(value = "/findEmployeeByBreak",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据’未关联‘查询员工信息，进而绑定关联",notes = " ",httpMethod = "get", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",paramType = "query",dataType = "int",required = true),
    })
    public R findEmployeeByBreak(Integer currentPage,Integer pageSize){
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<Employee> employees = sysUserService.findEmployeeByBreak();
        if(employees != null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(employees);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("查询失败");
            return r;
        }
    }


}

